package com.geovis.emergency.spd.controller.basic;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.CommonConstants;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.basic.service.IBasicDutyPostService;
import com.geovis.emergency.spd.entity.basic.entity.BasicDutyPost;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicDutyPostSaveDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicDutyPostUpdateDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 基础模块-值班值守岗位维护 前端控制器
 * </p>
 *
 * @author 王响
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/basic/basicDutyPost")
@RequiredArgsConstructor
@Api(value = "基础模块-值班值守岗位维护接口", tags = "基础模块-值班值守岗位维护接口")
@Slf4j
@Validated
public class BasicDutyPostController extends BaseController<IBasicDutyPostService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping({"/save"})
    public Result<String> save(@Validated @RequestBody BasicDutyPostSaveDTO saveDTO) {
        BasicDutyPost basicDutyPost = BeanUtil.toBean(saveDTO, BasicDutyPost.class);
        if (ObjectUtil.isEmpty(basicDutyPost.getParentId())) {
            basicDutyPost.setParentId(CommonConstants.TREE_ROOT_CODE);
        }
        baseService.save(basicDutyPost);
        return Result.ok();
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result<String> update(@Validated @RequestBody BasicDutyPostUpdateDTO updateDTO) {
        BasicDutyPost basicDutyPost = baseService.getById(updateDTO.getId());
        Assert.notNull(basicDutyPost, "id:{},库里没有查询要相关数据");
        BeanUtil.copyProperties(updateDTO, basicDutyPost);
        if (ObjectUtil.isEmpty(basicDutyPost.getParentId())) {
            basicDutyPost.setParentId(CommonConstants.TREE_ROOT_CODE);
        }
        baseService.updateById(basicDutyPost);
        return Result.ok();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody(required = false) Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = SwaggerConstants.LIST_VALUE, notes = SwaggerConstants.LIST_NOTES)
    @PostMapping("/list")
    public Result<List<BasicDutyPost>> list() {
        return Result.ok(baseService.list(Wrappers.lambdaQuery(BasicDutyPost.class).orderByAsc(BasicDutyPost::getIdx)));
    }


}
